package com.hunktimes.wholesale.net

import com.hunktimes.wholesale.BuildConfig
import com.hunktimes.wholesale.util.Constants
import okhttp3.OkHttpClient
import okhttp3.logging.HttpLoggingInterceptor
import retrofit2.Retrofit
import retrofit2.converter.gson.GsonConverterFactory
import java.util.concurrent.TimeUnit

/**
 * Author: Delete
 * Date: 2020/8/17 11:11
 * Description:
 *
 */
object ServiceCreater {
    private val okClientBuilder = OkHttpClient.Builder()
        .readTimeout(20, TimeUnit.SECONDS)
        .writeTimeout(20, TimeUnit.SECONDS)
        .connectTimeout(20, TimeUnit.SECONDS)
    private val retrofitBuilder = Retrofit.Builder()
        .baseUrl(Constants.base_url)
        .addConverterFactory(GsonConverterFactory.create())


    private val logInterceptor =
        HttpLoggingInterceptor().apply { level = HttpLoggingInterceptor.Level.BODY }

     fun <T> create(serviceClass: Class<T>): T {
        if (BuildConfig.DEBUG) {
            okClientBuilder.addInterceptor(logInterceptor)
        }
        return retrofitBuilder
            .client(okClientBuilder.build())
            .build()
            .create(serviceClass)
    }

    inline fun <reified T> create(): T {
        return create(T::class.java)
    }
}